package com.example.lab2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.lab2.db.AppDBOpenHelper;
import com.example.lab2.db.TodolistContract;

import java.util.Date;

public class AddActivity extends AppCompatActivity {
    public static final String TAG="AddActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        EditText tv=findViewById(R.id.editTextAdd);
        Button bt=findViewById(R.id.buttonAdd);
        bt.setOnClickListener(view -> {
            AppDBOpenHelper dbOpenHelper=new AppDBOpenHelper(getApplicationContext());
            SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
            ContentValues values=new ContentValues();
            values.put(TodolistContract.TodolistEntry.COL_NAME_DESCRIPTION,tv.getText().toString());
            values.put(TodolistContract.TodolistEntry.COL_NAME_DATE, new Date().toString());
            values.put(TodolistContract.TodolistEntry.COL_NAME_STATE,0);
            long newRowId=db.insert(TodolistContract.TodolistEntry.TABLENAME,null,values);
            Log.i(TAG, "inserting succeed: new ID---"+newRowId);
            db.close();
            finish();
        });
    }
}